﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Models;

namespace Data
{
    public class DifferentData:DataBase
    {
        public List<News> Search(string searchString)
        {
            var dataTable = ExecuteDataTable("Search", new List<SqlParameter> { new SqlParameter("@searchString", searchString) });
            if (dataTable.Rows.Count == 0) return null;
            
                var newsList = new List<News>();

                foreach (DataRow row in dataTable.Rows)
                {
                    var dbImages = new ImagesData();
                    var dbNews = new NewsData();
                    newsList.Add(new News
                                {
                                    Id = Convert.ToInt32(row["ID"]),
                                    Title = Convert.ToString(row["Title"]),
                                    Category = Convert.ToInt32(row["Category"]),
                                    Author = Convert.ToString(row["Author"]),
                                    Text = Convert.ToString(row["Text"]),
                                    PublishDate = CheckDbNull(row["PublishDate"]) as DateTime?,
                                    UpdateDate = CheckDbNull(row["UpdateDate"]) as DateTime?,
                                    Rating = dbNews.GetRating(Convert.ToInt32(row["ID"]))
                                });
                }
                return newsList;
        }
    }
}